package cn.demomaster.quickui.view.cardview;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.demomaster.quickcompat.widget.base.QuickViewAttribute;
import com.demomaster.quickcompat.widget.base.QuickViewUtil;

import cn.demomaster.quickui.R;
import cn.demomaster.quickui.view.drawable.RoundRectDrawableWithShadow2;

public class QuickCardView extends FrameLayout {
    public QuickCardView(@NonNull Context context) {
        super(context);
        init(null, 0);
    }

    public QuickCardView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs,0);
    }

    public QuickCardView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs,defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public QuickCardView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs,defStyleAttr);
    }

    ColorStateList colorBg;
    public void init(AttributeSet attrs, int defStyleAttr){
        if(attrs!=null) {
            QuickViewAttribute quickViewAttribute = QuickViewUtil.handleCustomAttrs(getContext(),attrs,defStyleAttr);
            if(quickViewAttribute.backgroundDrawable!=null) {
                colorBg = QuickViewUtil.getStateColorsFromDrawable(quickViewAttribute.backgroundDrawable);
            }
            initialize( colorBg, 20,25);
        }
    }

    private RoundRectDrawableWithShadow2 createBackground(Context context, ColorStateList backgroundColor, float radius, float elevation) {
        return new RoundRectDrawableWithShadow2( backgroundColor, getResources().getColor(com.demomaster.quickcompat.R.color.transparent_dark_55), getResources().getColor(com.demomaster.quickcompat.R.color.transparent),elevation, radius);
    }

    public void initialize(ColorStateList backgroundColor, float radius, float elevation) {
        RoundRectDrawableWithShadow2.mRoundRectHelper = new RoundRectDrawableWithShadow2.RoundRectHelper() {
            @Override
            public void drawRoundRect(Canvas canvas, RectF bounds, float cornerRadius, Paint paint) {
                canvas.drawRoundRect(bounds,10,10,paint);
            }
        };
        
        //final RoundRectDrawable background;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
           // background = new RoundRectDrawable(backgroundColor, radius);
            RoundRectDrawableWithShadow2 background2 = createBackground(getContext(), backgroundColor, radius, elevation);
            setBackground(background2);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.clipRect(-50, -50, 300, 300);
        super.onDraw(canvas);
    }
}
